package com.acwing.partition44;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/3/27 19:00
 */
public class AC4379两个闹钟_数论 {

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]);
        ss = reader.readLine().split(" ");
        int c = Integer.parseInt(ss[0]), d = Integer.parseInt(ss[1]);
        boolean flag = false;
        for (int i = Math.max(b, d); i <= 10100; i++) {
            if ((i - b) % a == 0 && (i - d) % c == 0) {
                writer.write(i + "\n");
                flag = true;
                break;
            }
        }
        if (!flag) writer.write("-1\n");
        writer.flush();
    }
}
